<?php

class siteadmin extends Controller
{

	protected function session(){
		parent::session();
		if( $_SESSION['admin'] !== '1' ) header('Location: /');
	}
	
	public function index()
	{
		$this->session();
		$orgs = new organization();
		$public = $orgs->getPublicOrgs();
		foreach( $public as $org ) {
			$org_id = $org['org_id'];	
			$orgboxes[$org_id]['logo'] = $org['logo'];
			$orgboxes[$org_id]['name'] = $org['name'];
			$orgboxes[$org_id]['description'] = $org['description'];
			$orgboxes[$org_id]['long_description'] = $org['long_description'];
			$orgboxes[$org_id]['org_id'] = $org_id;
		}
		$content = new BufferedView('siteadmin/orgs', array( 'orgboxes' => $orgboxes) );
		new View('template', 
			array(
				'title' => 'FEELS',
				'heading' => 'Courses',
				'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'orgs') ),
				'globalnav' => new BufferedView('org/globalnav'),
				'content' => $content
			)
		);	
	}
	
	public function add($param = NULL)
	{
		$this->session();
		$orgs = new organization();
		
		$content = new BufferedView('siteadmin/orgs', array( 'orgboxes' => $orgboxes) );
		new View('template', 
			array(
				'title' => 'FEELS',
				'heading' => 'Courses',
				'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'orgs') ),
				'globalnav' => new BufferedView('org/globalnav'),
				'content' => ($param === 'invalid' ? 'Please fill in all fields.' : '')
				. BufferedView::instance('siteadmin/forms/add', array(
					'action' => '/siteadmin/add_confirm'
				) )->getBuffer()
			)
		);	
	}	
	
	public function delete( $id, $confirm = NULL) {
		$this->session();
		$orgs = new organization();
		if( $confirm === NULL ) {
			new View('template', 
				array(
					'title' => 'FEELS',
					'heading' => 'Courses',
					'globalnav' => new BufferedView('org/globalnav'),
					'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'orgs') ),
					'content' => 'Are you sure you wish to delete this course? This will remove all of the courses info!'
					. BufferedView::instance('public/forms/confirm', array(
						'action' => "/siteadmin/delete/$id/confirm",
						'hiddenInputs' => array()
					) )->getBuffer()
				)
			);		
		}
		elseif( $confirm === 'confirm' ) {
			if( $_POST['confirm-yes'] ) {
				$orgs->deleteOrg($id);
				new View('template', 
					array(
						'title' => 'FEELS',
						'heading' => 'Courses',
						'globalnav' => new BufferedView('org/globalnav'),
						'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'orgs') ),
						'content' => 'Course has been deleted. Click <a href="/siteadmin">here</a> to go back to course list.'
					)
				);		
			}
			elseif( $_POST['confirm-no'] ) {
				header('Location: /siteadmin');
			}
		}

	}
	
	public function add_confirm () {
		$this->session();
		$orgs = new organization();
		if( !empty($_POST['name']) && !empty($_POST['desc']) && !empty($_POST['summary']) ) {
			$id = $orgs->insertOrg( $_POST['name'], $_POST['desc'], $_POST['summary'], '', '', '' );
			mkdir("./photos/$id");
			header('Location: /siteadmin');
		}
		else {
			header('Location: /siteadmin/add/invalid');
		}
	}

	public function assign( $orgName,  $id = NULL, $admin = NULL ) {
		$this->session();
		$data['title'] = 'Messages - FEELS';
		$userModel = new user();
		$adminModel = new admin();
		$orgModel = new organization();
		$orgid = $orgModel->getOrgId($orgName);
		$data['heading'] = "$orgname professor";
		if( $id === NULL ) {
			if( isset($_POST['search']{1}) ) {
				$results = $adminModel->findNotAdminByName( $_POST['search'], $orgid ) ;
				$admins = $adminModel->getAdmin( $orgid ) ;
				$data['content'] .= 
				BufferedView::instance('siteadmin/forms/search',array(
					"results" => (array) $results,
					"admins" => (array) $admins,
					'search' => $_POST['search'],
					"orgid" => $orgid,
					"orgName" => $orgName
				))->getBuffer();
				if( is_array( $results ) && count( $results ) === 0 ) $data['content'] .= 'No hits found';
			} else {
				$admins = $adminModel->getAdmin( $orgid ) ;
				$data['content'] .= 'Please enter at least 2 characters for the search' 
					. BufferedView::instance('siteadmin/forms/search',array(
						"results" => (array) $results,
						"admins" => (array) $admins,
						"orgid" => $orgid,
						'search' => $_POST['search'],
						"orgName" => $orgName
					))->getBuffer();
			}
		}
		elseif( isset($id ) ) {
			if( $admin === '1' ) {
				$adminModel->insertAdmin($id, $orgid);
				$memberModel = new member();
				$memberModel->insertMember($id, $orgid, gmdate('Y-m-d H:i:s'), 0 );
				header("Location: /siteadmin/assign/$orgName");
			}
			if( $admin === '0' ) {
				$adminModel->deleteAdminByStudent( $id, $orgid );
				header("Location: /siteadmin/assign/$orgName");
			}
		}

		
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = new BufferedView('siteadmin/nav', array('highlight'=>'orgs') );
		new View ("template", $data);
	}
	
		public function manageStudent( $orgid, $id = NULL, $admin = NULL ) {
		$this->session();
		$data['title'] = 'Messages - FEELS';
		$userModel = new user();
		$adminModel = new admin();
		$orgModel = new organization();
		$orgname = $orgModel->getOrgName($orgid);
		$data['heading'] = "$orgname professor";
		if( $id === NULL ) {
			if( isset($_POST['search']{1}) ) {
				$results = $adminModel->findNotAdminByName( $_POST['search'], $orgid ) ;
				$admins = $adminModel->getAdmin( $orgid ) ;
				$data['content'] .= 
				BufferedView::instance('siteadmin/forms/search',array(
					"results" => (array) $results,
					"admins" => (array) $admins,
					'search' => $_POST['search'],
					"orgid" => $orgid
				))->getBuffer();
				if( is_array( $results ) && count( $results ) === 0 ) $data['content'] .= 'No hits found';
			} else {
				$admins = $adminModel->getAdmin( $orgid ) ;
				$data['content'] .= 'Please enter at least 2 characters for the search' 
					. BufferedView::instance('siteadmin/forms/search',array(
						"results" => (array) $results,
						"admins" => (array) $admins,
						"orgid" => $orgid,
						'search' => $_POST['search']
					))->getBuffer();
			}
		}
		elseif( isset($id ) ) {
			if( $admin === '1' ) {
				$adminModel->insertAdmin($id, $orgid);
				$memberModel = new member();
				$memberModel->insertMember($id, $orgid, gmdate('Y-m-d H:i:s'), 0 );
				header("Location: /siteadmin/assign/$orgid");
			}
			if( $admin === '0' ) {
				$adminModel->deleteAdminByStudent( $id, $orgid );
				header("Location: /siteadmin/assign/$orgid");
			}
		}

		
		$data['globalnav'] = new BufferedView('org/globalnav');
		$data['usernav'] = new BufferedView('siteadmin/nav', array('highlight'=>'orgs') );
		new View ("template", $data);
	}
	
	public function filters( ) {
		$this->session();
		if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			file_put_contents('./lib/config/filter.txt', $_POST['filter'] );
			$output = '<p style="font-weight: bold; color: #fff;">List has been saved</p>';
		} 
		else $output = '';
		new View('template', array(
			'heading' => 'Filter',
			'content' => $output . BufferedView::instance( 'siteadmin/forms/filter', array( 
				'filter' => file_get_contents('./lib/config/filter.txt')
			) )->getBuffer(),
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav', array('highlight'=>'filters') ),
		) );
	}
	
	public function home( ) {
		$this->session();
		$output = '<p style="font-weight: bold; color: #fff;">WELCOME ADMIN</p>';
		new View('template', array(
			'heading' => 'WELCOME ADMIN!!',
			'content' => 'hello',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => new BufferedView('siteadmin/nav' ),
		) );
	}
	
}
	
